﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Diagnostics;
//using GorgonLibrary.Graphics;
using EngineOfEvermore.Common;

namespace EngineOfEvermore.GameApp.GameStates
{
    public class GameStateManager
    {
        #region public 
        public GameStateManager()
        {
            _states = new Dictionary<string, GameState>();
            _activeState = null;
            _isUpdating = false;
            _isDrawing = false;
            _stateActivationRequest = null;
        }

        public void Update( LogicUpdateEventArgs e )
        {
            _isUpdating = true;

            if ( _activeState != null )
            {
                _activeState.Update( e );
            }

            _isUpdating = false;

            if ( _stateActivationRequest != null )
            {
                _setStateActive( _stateActivationRequest );
                _stateActivationRequest = null;
            }
        }

        public void Draw( EngineOfEvermore.Imaging.IRenderer renderer )
        {
            _isDrawing = true;

            if ( _activeState != null )
            {
                _activeState.Draw( renderer );
            }

            _isDrawing = false;
        }

        public GameState GetState( String name )
        {
            GameState child;

            if ( _states.TryGetValue( name, out child ) )
            {
                return child;
            }
            else
            {
                return null;
            }
        }

        public GameState ActiveState
        {
            get { return _activeState; }
        }

        public void PostStateActivationRequest( GameState gs )
        {
            _stateActivationRequest = gs;
        }

        public ICollection<GameState> States
        {
            get { return _states.Values; }
        }

        public void AddState( GameState gs )
        {
            Debug.Assert( !_isUpdating && !_isDrawing );
            Debug.Assert( gs != null );
            Debug.Assert( !_states.ContainsKey( gs.Name ) );

            _states.Add( gs.Name, gs );
            
            gs.OnInit();

            foreach ( GameState sibling in _states.Values )
            {
                if ( sibling != gs )
                {
                    gs.OnSiblingAdded( sibling );
                    sibling.OnSiblingAdded( gs );
                }
            }
        }

        public void RemoveState( GameState gs )
        {
            Debug.Assert( !_isUpdating && !_isDrawing );
            Debug.Assert( gs != null );
            Debug.Assert( _states.ContainsKey( gs.Name ) );

            if ( gs == _activeState )
            {
                _setStateActive( null );
            }

            foreach ( GameState sibling in _states.Values )
            {
                if ( sibling != gs )
                {
                    gs.OnSiblingRemoved( sibling );
                    sibling.OnSiblingRemoved( gs );
                }
            }

            gs.OnDeInit();
            
            _states.Remove( gs.Name );
        }

        public void DeInit()
        {
            Debug.Assert( !_isUpdating && !_isDrawing );

            while ( _states.Values.Count > 0 )
            {
                IEnumerator<GameState> e = _states.Values.GetEnumerator();
                e.MoveNext();
                GameState child = e.Current;

                this.RemoveState( child );
            }
        }
        #endregion

        #region private 
        private void _setStateActive( GameState gs )
        {
            Debug.Assert( !_isUpdating && !_isDrawing );
            Debug.Assert( gs == null || _states.ContainsKey( gs.Name ) );

            if ( _activeState != null )
            {
                _activeState.OnDeActivate( gs );
            }

            GameState oldState = _activeState;
            _activeState = gs;

            if ( _activeState != null )
            {
                _activeState.OnActivate( oldState );
            }
        }

        private Dictionary<String, GameState> _states;
        private GameState _activeState;
        private bool _isUpdating;
        private bool _isDrawing;
        private GameState _stateActivationRequest;
        #endregion
    }
}
